package edu.gatech.volt2.drdoommgt.presenter;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.swing.JOptionPane;

import edu.gatech.volt2.drdoommgt.domain.DocOrder;
import edu.gatech.volt2.drdoommgt.domain.Doctor;
import edu.gatech.volt2.drdoommgt.domain.Labwork;
import edu.gatech.volt2.drdoommgt.domain.Nurse;
import edu.gatech.volt2.drdoommgt.domain.Patient;
import edu.gatech.volt2.drdoommgt.domain.Prescription;
import edu.gatech.volt2.drdoommgt.domain.TreatmentRecord;
import edu.gatech.volt2.drdoommgt.domain.VitalSigns;
import edu.gatech.volt2.drdoommgt.gui.ItreatmentRecordView;
import edu.gatech.volt2.drdoommgt.gui.TreatmentRecordDetails;
import edu.gatech.volt2.drdoommgt.system.DataBaseAccessor;

public class TreatmentRecordPresenter implements ActionListener{
	private DataBaseAccessor dba;
	private ItreatmentRecordView myView;
	private Patient myPatient;
	
	/**
	 * 
	 */
	public TreatmentRecordPresenter(Patient p, DataBaseAccessor db) {
		
		
		this.myView = (ItreatmentRecordView) new TreatmentRecordDetails();
		myView.addListener(this);
		myView.showSelf();
		myView.enableEditLabwork();
		myView.enableEditPrescription();
		
		// Load in the doctors
		dba = db;
		dba.connectToDatabase();
		
		myPatient = dba.find(Patient.class, p.getId());
		
		List<Doctor> docList = dba.getAllDoctors();
		List<Nurse> nurseList = dba.getAllNurses();
		myView.setDoctorList(docList);
		myView.setNurseList(nurseList);
		myView.enableDoctorList();
		myView.enableNurseList();

	}
	
	public TreatmentRecordPresenter(DataBaseAccessor db, TreatmentRecord tr) {
		this.myView = (ItreatmentRecordView) new TreatmentRecordDetails();
		myView.addListener(this);
		myView.showSelf();
		
		
		// Load in the doctors
		dba = db;
		if ( !dba.isConnected() )
			dba.connectToDatabase();
		List<Doctor> docList = dba.getAllDoctors();
		List<Nurse> nurseList = dba.getAllNurses();
		myView.setDoctorList(docList);
		myView.setNurseList(nurseList);
		myView.setSelectedDoctor(tr.getTreatingDoc());
		myView.setSelectedNurse(tr.getNurse());
		myView.enableDoctorList();
		myView.enableNurseList();
	}
	


	@Override
	public void actionPerformed(ActionEvent e) {
		if ( e.getSource() == myView.getAddPrescriptionButton()){
			@SuppressWarnings("unused")
			PrescriptionPresenter prescriptionAdd = new PrescriptionPresenter(this);
		}else if ( e.getSource() == myView.getRemovePrescriptionButton()){
			myView.getPrescriptionList().removeElement(myView.getPrescriptionListList().getSelectedValue());
		}else if ( e.getSource() == myView.getAddLabworkButton()){
			@SuppressWarnings("unused")
			LabworkPresenter lw = new LabworkPresenter(this);
		}else if ( e.getSource() == myView.getRemoveLabworkButton()){
			myView.getLabworkList().removeElement(myView.getLabworkListList().getSelectedValue());
		}else if ( e.getSource() == myView.getOkButton()){
			
			// Save 
			TreatmentRecord tr = new TreatmentRecord();
			tr.setComplaint(myView.getComplaintField().getText());
			tr.setDiagnosis(myView.getDiagnosisField().getText());
			tr.setNotes(myView.getNotes());
			tr.setDate(myView.getDate());
			
			Doctor treatingDoc = this.dba.find(Doctor.class, ((Doctor)myView.getDoctorField().getSelectedItem()).getId());
			tr.setTreatingDoc(treatingDoc);
			Nurse nurse = this.dba.find(Nurse.class, ((Nurse)myView.getNurseField().getSelectedItem()).getId());
			tr.setNurse(nurse);
			
			DocOrder docOrders = new DocOrder();
			
			if ( myView.getLabworkList().size() > 0 ){
				List<Labwork> labList = new ArrayList<Labwork>();
				for ( int i=0;i<myView.getLabworkList().getSize(); i++ ){
					Labwork labw = (Labwork)myView.getLabworkList().getElementAt(i);
					labList.add(labw);
				}
				docOrders.setLabwork(labList);
			}
			if ( myView.getPrescriptionList().size() > 0 ){
				List<Prescription> presList = new ArrayList<Prescription>();
				Object[] genericList = myView.getPrescriptionList().toArray();
				for ( Object l : genericList ){
					presList.add((Prescription)l);
				}
				docOrders.setPrescription(presList);
			}
			
			docOrders.setFollowup(myView.getFollowUpField().getText());
			tr.setDocOrders(docOrders);
			
			VitalSigns vs = new VitalSigns();
			vs.setHeightFeet(myView.getHeightFeet());
			vs.setHeightInches(myView.getHeightInches());
			vs.setPB(myView.getBloodPressue());
			vs.setTemp(myView.getTemperature());
			vs.setWeight(myView.getWeight());
			vs.setPulse(myView.getPulse());
			
			tr.setVital(vs);
			
			myPatient.addTreatmentRecord(tr);
			this.dba.save(tr);
			this.dba.save(myPatient);
			
			
			
			JOptionPane.showMessageDialog(null, "Treatmnt Record Saved");
			myView.hideSelf();
		}else if ( e.getSource() == myView.getCancelButton()){
			myView.hideSelf();
		}
	}
	
	
	public void addNewPrescription(Prescription prescription){
		myView.getPrescriptionList().addElement(prescription);
	}

	public void addNewLabwork(Labwork lw) {
		myView.getLabworkList().addElement(lw);
	}

	public DataBaseAccessor getConnection() {
		return dba;
	}

	public void disableDocOrder() {
		myView.disableDocOrder();
	}
	

}
